/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.service;

import com.andersen.web.exception.ServiceException;
import com.andersen.database.dao.StateDao;
import com.andersen.database.entity.StateEntity;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Corvus 11
 */
@Service
public class StateService {
    
    public static final int SORT_DIRECTION_ASC = 0;
    public static final int SORT_DIRECTION_DESC = 1;
    
    @Autowired
    StateDao dao;
    
    private static final Logger LOG = Logger.getLogger(StateService.class);
 
    public ArrayList<StateEntity> queryForComponentTable(int fromItem, 
                                                         int count, 
                                                         int sortBy, 
                                                         int sortOrder, 
                                                         List<Long> cathegories) {
        return dao.queryForComponentTable(fromItem, count, sortBy, sortOrder, cathegories);
    }
    
    
    /*public long createFirstState(long newComponentId, long officeId, long employeeId, long computerId, Date fromDate, char status) {
        StateEntity newState = new StateEntity();
        newState.setComponentId(newComponentId);
        newState.setOfficeId(officeId);
        newState.setEmployeeId(employeeId);
        newState.setFromDate(fromDate);
        newState.setStatus(status);
        newState.setComputerId(computerId);
        
        return dao.insertState(newState);
    }*/
    
    public long countCurrentStates(List<Long> cathegories) {
        return dao.countCurrentStates(cathegories);
    }
    
    private StateEntity closePreviousState(StateEntity previousState, Date closeDate) throws ServiceException {
        if (closeDate.before(previousState.getFromDate()))
            throw new ServiceException();
        previousState.setToDate(closeDate);
        LOG.info("StateService.closePreviousState() - closed state: " + previousState);
        dao.updateState(previousState);
        StateEntity newState = new StateEntity();
        newState.setComponentId(previousState.getComponentId());
        newState.setComputerId(previousState.getComputerId());
        newState.setEmployeeId(previousState.getEmployeeId());
        newState.setFromDate(previousState.getToDate());
        newState.setOfficeId(previousState.getOfficeId());
        return newState;
    }
    
    public long moveComponentToComputer(long componentId, long newComputerId, Date moveDate, long officeId) throws ServiceException {
        StateEntity oldSt = dao.getLastStateOfComponent(componentId);
        StateEntity newSt = closePreviousState(oldSt, moveDate);
        newSt.setComputerId(newComputerId);
        if (officeId != 0l) newSt.setOfficeId(officeId);
        return dao.insertState(newSt);
    }
    
    public long moveComponentToEmployee(long componentId, long newEmployeeId, Date moveDate, long officeId) throws ServiceException {
        StateEntity oldSt = dao.getLastStateOfComponent(componentId);
        StateEntity newSt = closePreviousState(oldSt, moveDate);
        newSt.setEmployeeId(newEmployeeId);
        if (officeId != 0l) newSt.setOfficeId(officeId);
        return dao.insertState(newSt);
    }
    
    public long moveComponentToOffice(long componentId, long newOfficeId, Date moveDate) throws ServiceException {
        StateEntity oldSt = dao.getLastStateOfComponent(componentId);
        StateEntity newSt = closePreviousState(oldSt, moveDate);
        newSt.setOfficeId(newOfficeId);
        return dao.insertState(newSt);
    }
    
    public long changeComponentStatus(long componentId, char newStatus, Date changeDate) throws ServiceException {
        StateEntity oldSt = dao.getLastStateOfComponent(componentId);
        LOG.info("StateService.changeComponentStatus() - old state: " + oldSt.toString());
        StateEntity newSt = closePreviousState(oldSt, changeDate);
        LOG.info("StateService.changeComponentStatus() - new state: " + newSt.toString());
        newSt.setStatus(newStatus);
        return dao.insertState(newSt);
    }
    
    public StateEntity getLastStateOfComponent(long componentId) {
        return dao.getLastStateOfComponent(componentId);
    }
}
